//
//  DestinTableViewCtrl.m
//  LvXingPai
//
//  Created by _Junxee on 16/8/9.
//  Copyright © 2016年 _Junxee. All rights reserved.
//

#import "DestinTableViewCtrl.h"
#import "DestinTableViewCell.h"
#import "DestinModel.h"
#import "AFNetworking.h"
#import "DestinNextCtrl.h"
#import "XXXRoundMenuButton.h"

@interface DestinTableViewCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet XXXRoundMenuButton *SupriseView;
@property (weak, nonatomic) IBOutlet UITableView *MyTableView;
@property(nonatomic,strong) NSMutableArray * dataArr;

@end

@implementation DestinTableViewCtrl

- (NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //改变状态栏样式
    [self preferredStatusBarStyle];
    //tableview顶格
    self.edgesForExtendedLayout = UIRectEdgeNone;
    //限制tableview的上下拉
    self.MyTableView.bounces = NO;
    //隐藏cell的分割线
    self.MyTableView.separatorStyle = NO;

    [self.MyTableView insertSubview:self.SupriseView atIndex:999];
    [self btnSuprise];
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    //隐藏导航栏
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}


//改变状态栏样式
-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

//获取数据
-(void)loadData{
    
    AFHTTPRequestOperationManager * Manger = [AFHTTPRequestOperationManager manager];
    
    [Manger GET:Destin_Main parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"获取数据成功");
        NSArray *arr  = responseObject[@"result"];
        for (NSDictionary *dict1 in arr) {
            
            NSArray *arr1 = dict1[@"images"];
            NSDictionary *dict2 = arr1[0];
            
            DestinModel *model = [[DestinModel alloc]init];
            model.url = dict2[@"url"];
            model.zhName = dict1[@"zhName"];
            model.enName = dict1[@"enName"];
            model.pushId = dict1[@"id"];
            
            
            NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
            
            NSString *str = [numberFormatter stringFromNumber:dict1[@"commoditiesCnt"]];
            
            model.commoditiesCnt = str;
            
            [self.dataArr addObject:model];
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.MyTableView reloadData];
        });
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"f");
    }];
}


#pragma mark -- tableview代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DestinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"destin" forIndexPath:indexPath];
    
    DestinModel *model = self.dataArr[indexPath.row];
    [cell.MainImageView sd_setImageWithURL:[NSURL URLWithString:model.url]];
    cell.ChineseNameLabel.text = model.zhName;
    cell.EnglishNameLabel.text = model.enName;
    cell.CountLabel.text = model.commoditiesCnt;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DestinNextCtrl *nextVC = [self.storyboard instantiateViewControllerWithIdentifier:@"next"];
    nextVC.Amodel = self.dataArr[indexPath.row];
//    隐藏下一个界面系统的tabbar
//    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:nextVC animated:YES];
    //返回当前界面时显示tabbar
//    self.hidesBottomBarWhenPushed = NO;
    
}

#pragma mark -- 点击弹出其他目的地
-(void)btnSuprise{
    
    self.SupriseView.centerButtonSize = CGSizeMake(44, 44);
    self.SupriseView.centerIconType = XXXIconTypeUserDraw;
    self.SupriseView.tintColor = [UIColor whiteColor];
    self.SupriseView.jumpOutButtonOnebyOne = YES;
    
    [self.SupriseView setDrawCenterButtonIconBlock:^(CGRect rect, UIControlState state) {
        
        if (state == UIControlStateNormal)
        {
            UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake((rect.size.width - 15)/2, rect.size.height/2 - 5, 15, 1)];
            [UIColor.whiteColor setFill];
            [rectanglePath fill];
            
            UIBezierPath* rectangle2Path = [UIBezierPath bezierPathWithRect: CGRectMake((rect.size.width - 15)/2, rect.size.height/2, 15, 1)];
            [UIColor.whiteColor setFill];
            [rectangle2Path fill];
            
            UIBezierPath* rectangle3Path = [UIBezierPath bezierPathWithRect: CGRectMake((rect.size.width - 15)/2, rect.size.height/2 + 5, 15, 1)];
            [UIColor.whiteColor setFill];
            [rectangle3Path fill];
        }
    }];
    
    [self.SupriseView loadButtonWithIcons:@[
                                          [UIImage imageNamed:@"icon_goodsDetail_bookquit"],
                                          [UIImage imageNamed:@"icon_goodsDetail_bookquit"],
                                          [UIImage imageNamed:@"icon_goodsDetail_bookquit"],
                                          [UIImage imageNamed:@"icon_goodsDetail_bookquit"],
                                          [UIImage imageNamed:@"icon_goodsDetail_bookquit"],
                                          [UIImage imageNamed:@"icon_goodsDetail_bookquit"],
                                          [UIImage imageNamed:@"icon_goodsDetail_bookquit"],
                                          [UIImage imageNamed:@"icon_goodsDetail_bookquit"],
                                          [UIImage imageNamed:@"icon_goodsDetail_bookquit"]
                                          
                                          ] startDegree:0 layoutDegree:M_PI*2*7/8];
    
    [self.SupriseView setButtonClickBlock:^(NSInteger idx) {
        
        NSLog(@"button %@ clicked !",@(idx));
    }];
    
}

@end
